home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr40 / radserv.zip / TTYSETUP.C < prev    next >
C/C++ Source or Header  |  1995-01-24  |  10KB  |  393 lines

  1. /*
  2.  * %W% %E% %U%
  3.  * Sample code for communication with external device via a supported
  4.  * serial port. The routines in this file are used for setting up
  5.  * the serial port parameters. Externally visible interface is
  6.  * SetPortParams(). A dialog box is used to solicit changes.
  7.  * Results will be used to update/set a tty_t struct.
  8.  */
  9.  
  10. #include "resource.h"
  11. #include "radextrn.h"
  12. #include "vsttype.h"
  13.  
  14. ttyadj_t ttyadj = { 0 };
  15.  
  16. radparam_t rigInfo = {
  17.     {
  18.         2, 4800, 8, NOPARITY, ONESTOPBIT, 0, 0, { 0 }, &ttyadj
  19.     },
  20.     { 0 }
  21. };
  22.  
  23. static int portflags;
  24. static char portname[48];
  25. static char baudrate[16];
  26. static char icdelay[16];
  27. static char optionals[32];
  28. static tty_t *ttyp;
  29.  
  30. #define RAD_RTSCTS       0x000001 /* MUST BE SAME AS TTY_RTSCTS */
  31. #define RAD_DSRDTR       0x000002 /* MUST BE SAME AS TTY_DSRDTR */
  32. #define RAD_XONXOFF      0x000004 /* MUST BE SAME AS TTY_XONXOFF */
  33. #define RAD_RFU          0x000008
  34. #define RAD_DBITS_7      0x000010
  35. #define RAD_DBITS_8      0x000020
  36. #define RAD_STOP_1       0x000040
  37. #define RAD_STOP_15      0x000080
  38. #define RAD_STOP_2       0x000100
  39. #define RAD_PARITY_NONE  0x000200
  40. #define RAD_PARITY_ODD   0x000400
  41. #define RAD_PARITY_EVEN  0x000800
  42. #define RAD_PARITY_MARK  0x001000
  43. #define RAD_PARITY_SPACE 0x002000
  44. #define RAD_DBITS_6      0x004000
  45.  
  46. button_t BtnTab[] = {
  47.     RAD_DBITS_6,      IDB_DATABITS_6,  0,
  48.     RAD_DBITS_7,      IDB_DATABITS_7,  0,
  49.     RAD_DBITS_8,      IDB_DATABITS_8,  0,
  50.     RAD_STOP_1,       IDB_STOPBITS_1,  0,
  51.     RAD_STOP_15,      IDB_STOPBITS_15, 0,
  52.     RAD_STOP_2,       IDB_STOPBITS_2,  0,
  53.     RAD_PARITY_NONE,  IDB_PARITY_NONE, 0,
  54.     RAD_PARITY_ODD,   IDB_PARITY_ODD, 0,
  55.     RAD_PARITY_EVEN,  IDB_PARITY_EVEN, 0,
  56.     RAD_PARITY_MARK,  IDB_PARITY_MARK, 0,
  57.     RAD_PARITY_SPACE, IDB_PARITY_SPACE, 0,
  58.     RAD_RTSCTS,       IDB_HS_RTSCTS, 0,
  59.     RAD_DSRDTR,       IDB_HS_DTR, 0,
  60.     RAD_XONXOFF,      IDB_HS_XONXOFF, 0,
  61.     -1,             -1, 0,
  62. };
  63.  
  64.  
  65. static void
  66. ttytocontrols()
  67. {
  68.     register tty_t *tp = ttyp;
  69.     
  70.     portflags = tp->tt_flags & 07;
  71.     sprintf(portname,"COM%d", tp->tt_portno);
  72.     sprintf(baudrate,"%d", tp->tt_baudrate);
  73.     sprintf(icdelay,"%d", tp->tt_icdelay);
  74.     strncpy(optionals, tp->tt_optparams, sizeof(optionals) -1);
  75.     
  76.     if (tp->tt_stopbits == ONESTOPBIT)
  77.         portflags |= RAD_STOP_1;
  78.     if (tp->tt_stopbits == ONE5STOPBITS)
  79.         portflags |= RAD_STOP_15;
  80.     if (tp->tt_stopbits == TWOSTOPBITS)
  81.         portflags |= RAD_STOP_2;
  82.  
  83.     if (tp->tt_databits == 6)
  84.         portflags |= RAD_DBITS_6;
  85.     if (tp->tt_databits == 7)
  86.         portflags |= RAD_DBITS_7;
  87.     if (tp->tt_databits == 8)
  88.         portflags |= RAD_DBITS_8;
  89.  
  90.     if (tp->tt_parity == NOPARITY)
  91.         portflags |= RAD_PARITY_NONE;
  92.     if (tp->tt_parity == ODDPARITY)
  93.         portflags |= RAD_PARITY_ODD;
  94.     if (tp->tt_parity == EVENPARITY)
  95.         portflags |= RAD_PARITY_EVEN;
  96.     if (tp->tt_parity == MARKPARITY)
  97.         portflags |= RAD_PARITY_MARK;
  98.     if (tp->tt_parity == SPACEPARITY)
  99.         portflags |= RAD_PARITY_SPACE;
  100. }
  101.  
  102. static void
  103. controlstotty()
  104. {
  105.     register tty_t *tp = ttyp;
  106. #ifdef _DEBUG_
  107.     diag("Baud Rate %s port %s\n", baudrate, portname);
  108. #endif /* _DEBUG_ */
  109.  
  110.     sscanf(baudrate,"%d", &tp->tt_baudrate);
  111.     
  112.     UpperCase(portname);
  113.     sscanf(portname,"COM%d",&tp->tt_portno);
  114.     
  115.     sscanf(icdelay,"%d", &tp->tt_icdelay);
  116.     
  117.     if (tp->tt_optparams)
  118.         GlobalFree((HANDLE)tp->tt_optparams);
  119.         
  120.     strncpy(tp->tt_optparams, optionals, sizeof(tp->tt_optparams) -1);
  121.     
  122.     if (portflags & RAD_DBITS_6)
  123.         tp->tt_databits = 6;
  124.     if (portflags & RAD_DBITS_7)
  125.         tp->tt_databits = 7;
  126.     if (portflags & RAD_DBITS_8)
  127.         tp->tt_databits = 8;
  128.         
  129.     if (portflags & RAD_STOP_1)
  130.         tp->tt_stopbits = ONESTOPBIT;
  131.     if (portflags & RAD_STOP_15)
  132.         tp->tt_stopbits = ONE5STOPBITS;
  133.     if (portflags & RAD_STOP_2)
  134.         tp->tt_stopbits = TWOSTOPBITS;
  135.  
  136.     if (portflags & RAD_PARITY_NONE)
  137.         tp->tt_parity = NOPARITY;
  138.     if (portflags & RAD_PARITY_ODD)
  139.         tp->tt_parity = ODDPARITY;
  140.     if (portflags & RAD_PARITY_EVEN)
  141.         tp->tt_parity = EVENPARITY;
  142.     if (portflags & RAD_PARITY_MARK)
  143.         tp->tt_parity = MARKPARITY;
  144.     if (portflags & RAD_PARITY_SPACE)
  145.         tp->tt_parity = SPACEPARITY;
  146.     tp->tt_flags = portflags & 07;
  147. }
  148.     
  149.  
  150. void
  151. setbutton(HWND hwnd, int id, BOOL flag)
  152. {
  153.     SendDlgItemMessage(hwnd, id, BM_SETSTATE, flag ? (WPARAM) 1 : 0, (LPARAM) 0);
  154. }
  155.     
  156.  
  157. void
  158. setbuttons(HWND hwnd, int state, button_t *btab)
  159. {
  160.     register button_t *btp;
  161.     int p;
  162.  
  163.     for (btp = btab; btp->fval != -1; btp++) {
  164.         p = btp->fval & state;
  165.         setbutton(hwnd, btp->bval, p ? TRUE : FALSE);
  166.     }
  167. }
  168.  
  169.  
  170. static int
  171. checkbuttons(hwnd, wParam)
  172. HWND hwnd;
  173. WPARAM wParam;
  174. {
  175.     int cmd;
  176.  
  177.     cmd = LOWORD(wParam);
  178.     
  179.     switch (cmd) {
  180.     case IDOK:
  181.     case IDCANCEL:
  182.         return cmd;
  183.     default:
  184.         break;
  185.     }
  186.     
  187.     if (HIWORD(wParam) != BN_CLICKED)
  188.         return -1;
  189.  
  190.     switch (cmd) {
  191.     case IDB_STOPBITS_1:
  192.         portflags &= ~(RAD_STOP_2|RAD_STOP_15);
  193.         portflags |= RAD_STOP_1;
  194.         goto bset;
  195.  
  196.     case IDB_STOPBITS_15:
  197.         portflags &= ~(RAD_STOP_1|RAD_STOP_2);
  198.         portflags |= RAD_STOP_15;
  199.         goto bset;
  200.  
  201.     case IDB_STOPBITS_2:
  202.         portflags &= ~(RAD_STOP_1|RAD_STOP_15);
  203.         portflags |= RAD_STOP_2;
  204.         goto bset;
  205.  
  206.     case IDB_PARITY_NONE:
  207.         portflags &= ~(RAD_PARITY_ODD|RAD_PARITY_EVEN|RAD_PARITY_MARK|RAD_PARITY_SPACE);
  208.         portflags |= RAD_PARITY_NONE;
  209.         goto bset;
  210.  
  211.     case IDB_PARITY_ODD:
  212.         portflags &= ~(RAD_PARITY_NONE|RAD_PARITY_EVEN|RAD_PARITY_MARK|RAD_PARITY_SPACE);
  213.         portflags |= RAD_PARITY_ODD;
  214.         goto bset;
  215.         
  216.     case IDB_PARITY_EVEN:
  217.         portflags &= ~(RAD_PARITY_NONE|RAD_PARITY_ODD|RAD_PARITY_MARK|RAD_PARITY_SPACE);
  218.         portflags |= RAD_PARITY_EVEN;
  219.         goto bset;
  220.         
  221.     case IDB_PARITY_MARK:
  222.         portflags &= ~(RAD_PARITY_NONE|RAD_PARITY_ODD|RAD_PARITY_EVEN|RAD_PARITY_SPACE);
  223.         portflags |= RAD_PARITY_MARK;
  224.         goto bset;
  225.         
  226.     case IDB_PARITY_SPACE:
  227.         portflags &= ~(RAD_PARITY_NONE|RAD_PARITY_ODD|RAD_PARITY_EVEN|RAD_PARITY_MARK);
  228.         portflags |= RAD_PARITY_SPACE;
  229.         goto bset;
  230.  
  231.     case IDB_HS_RTSCTS:
  232.         portflags ^= RAD_RTSCTS;
  233.         goto bset;
  234.         
  235.     case IDB_HS_DTR:
  236.         portflags ^= RAD_DSRDTR;
  237.         goto bset;
  238.         
  239.     case IDB_HS_XONXOFF:
  240.         portflags ^= RAD_XONXOFF;
  241.         goto bset;
  242.         
  243.     case IDB_DATABITS_6:
  244.         portflags &= ~(RAD_DBITS_7|RAD_DBITS_8);
  245.         portflags |= RAD_DBITS_6;
  246.         goto bset;
  247.         
  248.     case IDB_DATABITS_7:
  249.         portflags &= ~(RAD_DBITS_6|RAD_DBITS_8);
  250.         portflags |= RAD_DBITS_7;
  251.         goto bset;
  252.             
  253.     case IDB_DATABITS_8:
  254.         portflags &= ~(RAD_DBITS_6|RAD_DBITS_7);
  255.         portflags |= RAD_DBITS_8;
  256.         /* Fall Through to bset */
  257.  
  258.     bset:
  259.         setbuttons(hwnd, portflags, BtnTab);
  260.         break;
  261.         
  262.      default: /* Warning: must return -1 */
  263.         break;
  264.      }
  265.      return -1;
  266. }
  267.  
  268.  
  269. static void
  270. setControls(hwnd)
  271. HWND hwnd;
  272. {    
  273.     SendDlgItemMessage(hwnd, IDC_PORTPARAMS_PORT, WM_SETTEXT, (WPARAM) 0,
  274.         (LPARAM) portname);
  275.     SendDlgItemMessage(hwnd, IDC_PORTPARAMS_BAUD, WM_SETTEXT, (WPARAM) 0,
  276.         (LPARAM) baudrate);
  277.     SendDlgItemMessage(hwnd, IDC_PORTPARAMS_OPTIONALS, WM_SETTEXT, (WPARAM) 0,
  278.         (LPARAM) optionals);
  279.     SendDlgItemMessage(hwnd, IDC_PORTPARAMS_ICDELAY, WM_SETTEXT, (WPARAM) 0,
  280.         (LPARAM) icdelay);
  281.  
  282.     setbuttons(hwnd, portflags, BtnTab);
  283. }
  284.  
  285. static void
  286. getControls(hwnd)
  287. HWND hwnd;
  288. {
  289.     extern char *GetEditText(HWND, int, char *, int);
  290.  
  291.     GetEditText(hwnd, IDC_PORTPARAMS_PORT, portname, sizeof(portname));
  292.     GetEditText(hwnd, IDC_PORTPARAMS_BAUD, baudrate, sizeof(baudrate));
  293.     GetEditText(hwnd, IDC_PORTPARAMS_ICDELAY, icdelay, sizeof(icdelay));
  294.     GetEditText(hwnd, IDC_PORTPARAMS_OPTIONALS, optionals, sizeof(optionals));
  295. }
  296.  
  297. static BOOL CALLBACK
  298. PortParamsProc(hwnd, message, wParam, lParam)
  299. HWND hwnd;
  300. UINT message;
  301. WPARAM wParam;
  302. LPARAM lParam;
  303. {
  304.     POINT *p;
  305.     int cmd;
  306.  
  307.     if (message == WM_MOUSEMOVE)
  308.         return TRUE;
  309.  
  310.     switch(message) {
  311.         
  312.     case WM_INITDIALOG:
  313.         p = DialogPos(Gwnd, hwnd);
  314.         SendMessage(hwnd, DM_SETDEFID, (WPARAM) IDNONE, (LPARAM)0 );
  315.         SendMessage(GetDlgItem(hwnd, IDOK), WM_SETFOCUS, 0, (LPARAM)0);
  316.         SetWindowPos(hwnd, 0, (int)p->x, (int)p->y, 0, 0,
  317.             SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
  318.         setControls(hwnd);
  319.         return TRUE;
  320.  
  321.         case WM_COMMAND:
  322.             if ((cmd = checkbuttons(hwnd, wParam)) >= 0) {
  323.                 switch (cmd) {
  324.                 case IDOK:
  325.                     getControls(hwnd);
  326.                     EndDialog(hwnd, (WPARAM)TRUE);
  327.                     return TRUE;
  328.  
  329.                 case IDCANCEL:
  330.                     EndDialog(hwnd, (WPARAM)FALSE);
  331.                     return TRUE;
  332.  
  333.                 default:
  334.                     break;
  335.                 }
  336.             }
  337.             break;
  338.  
  339.         default:
  340.             break;
  341.         }
  342.         return FALSE;
  343. }
  344.  
  345.        
  346. char *
  347. GetEditText(hwnd, ItemId, buf, maxlen)
  348. HWND hwnd;
  349. int ItemId;
  350. char *buf;
  351. int maxlen;
  352. {
  353.     int i;
  354.     char tbuf[80], *cp;
  355.     
  356.     i = SendDlgItemMessage(hwnd, ItemId, (UINT)WM_GETTEXTLENGTH, (WPARAM) 0, (LPARAM) 0);
  357.     if (i <=0 || i >= sizeof(tbuf)-1)
  358.         return NULL;
  359.     else if (i > 0) {
  360.         SendDlgItemMessage(hwnd, ItemId, (UINT)WM_GETTEXT, (WPARAM) i+1, (LPARAM) tbuf);
  361.         tbuf[i+1] = 0; 
  362.         cp = stripLeadingSpace(tbuf);
  363.         if (!buf) {
  364.             stripTrailingSpace(cp);
  365.             buf = saveString(cp);
  366.         }
  367.         else {
  368.             strncpy(buf, cp, MIN(sizeof(tbuf), maxlen));
  369.             stripTrailingSpace(buf);
  370.         }
  371.     }
  372.     return buf;
  373. }
  374.  
  375. void
  376. PortParams()
  377. {
  378.     int r;
  379.     extern BOOL TTYProc(tty_t *);
  380.  
  381.     ttyp = &rigInfo.tty;
  382.     ttytocontrols();
  383.  
  384.     r = DialogBox(NULL, MAKEINTRESOURCE(IDD_PORTPARAMS), Gwnd, PortParamsProc);
  385.  
  386. #ifdef _DEBUG_
  387.     diag("End Dialog\n");
  388. #endif /* _DEBUG_ */
  389.  
  390.     if (r)
  391.         controlstotty();
  392. }
  393.